package com.hxxzt.hikari.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxxzt.hikari.common.exception.NikoException;
import com.hxxzt.hikari.entity.Tree;
import com.hxxzt.hikari.entity.User;
import com.hxxzt.hikari.mapper.UserMapper;
import com.hxxzt.hikari.service.IUserService;
import com.hxxzt.hikari.utils.TreeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-01-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> queryAll() {
        return userMapper.selectList(null);
    }

    @Override
    public User queryById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> queryByUser(User user) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);

        return userMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<User> queryPage(Integer page, Integer limit) {
        IPage<User> userPage = new Page<>(page, limit, true);//参数一是当前页，参数二是每页个数，参数三为是否显示总条数
        return userMapper.selectPage(userPage, new LambdaQueryWrapper<User>().orderByDesc(User::getUserId));

    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public JSONObject findSon() throws NikoException {

        JSONObject data = new JSONObject();
        try {


            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
            wrapper
                    .select(User::getUserId, User::getParentId, User::getUsername, User::getPhone);

            List<User> users = userMapper.selectList(wrapper);
            List<Tree<User>> trees = new ArrayList<>();
            List<Integer> ids = new ArrayList<>();
            buildTrees(trees, users, ids);

            data.put("ids", ids);
            data.put("rows", TreeUtil.build(trees));
            return data;

        } catch (Exception e) {

            throw new NikoException(e.getMessage());
        }
    }

    @Override
    public JSONObject findSonByUserId(Integer userId) throws NikoException {
        JSONObject data = new JSONObject();
        try {


            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
            wrapper
                    .select(User::getUserId, User::getParentId, User::getUsername, User::getPhone)
                    .eq(User::getParentId, userId);

            List<User> users = userMapper.selectList(wrapper);
            List<Tree<User>> trees = new ArrayList<>();
            List<Integer> ids = new ArrayList<>();
            buildTrees(trees, users, ids);

            data.put("ids", ids);
            data.put("rows", trees);
            return data;

        } catch (Exception e) {

            throw new NikoException(e.getMessage());
        }
    }

    private void buildTrees(List<Tree<User>> trees, List<User> users, List<Integer> ids) {
        users.forEach(user -> {
            ids.add(user.getUserId());
            Tree<User> tree = new Tree<>();

            tree.setParentId(user.getParentId());
            tree.setUsername(user.getUsername());
            tree.setPhone(user.getPhone());
            tree.setUserId(user.getUserId());
            trees.add(tree);
        });
    }


}
